package com.taxinfo.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxinfo.common.ClientSession;
import com.taxinfo.component.alert.MyAlert;
import com.taxinfo.dao.DecDocumentRecMapper;
import com.taxinfo.pojo.dto.DecDocumentRecDto;
import com.taxinfo.pojo.dto.LoginDto;
import com.taxinfo.pojo.vo.DecDocumentRec;
import com.taxinfo.pojo.vo.RobotOutParams;
import com.taxinfo.pojo.vo.RobotPlanBean;
import com.taxinfo.pojo.vo.RobotProjectInfoBean;
import com.taxinfo.service.DecDocumentRecService;
import com.taxinfo.utils.DateUtil;
import com.taxinfo.utils.HttpClientUtil;
import com.taxinfo.view.declaration.DecDocumentTable;
import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanghy
 * @since 2025-01-20
 */
@Service("DecDocumentRecServiceImpl")
public class DecDocumentRecServiceImpl extends ServiceImpl<DecDocumentRecMapper, DecDocumentRec> implements DecDocumentRecService {

    @Autowired
    private DecDocumentRecMapper decDocumentRecMapper;

    @Override
    public List<DecDocumentRecDto> selectAllData() {
        return decDocumentRecMapper.selectAllData();
    }

    @Override
    public List<DecDocumentRecDto> selectDataByParentId(String id) {
        return decDocumentRecMapper.selectDataByParentId(id);
    }

    @Override
    public List<DecDocumentRecDto> selectDataByCondition(String decYear, String pSubType) {
        return decDocumentRecMapper.selectDataByCondition(decYear, pSubType);
    }

    @Override
    public String callRobot(DecDocumentTable table, LoginDto loginDto) {

        String filePath = table.getSelectedData().getDecFileAddr();
        String fileId = table.getSelectedData().getId();
        String sbYear = table.getSelectedData().getDecYear();
        String sbYearType = table.getSelectedData().getSbType();
        String fileName = table.getSelectedData().getDecFileName();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("fun", "new_task");
        params.put("token", loginDto.getTokens());
        List<RobotPlanBean> data = new ArrayList();
        params.put("data", data);
        List<RobotProjectInfoBean> task_robots = new ArrayList();
        List<RobotOutParams> robot_ipars = new ArrayList();
          //计划信息
        RobotOutParams outParams0 = new RobotOutParams();
        outParams0.setPins_pin_name("filepath");
        outParams0.setPins_pin_defval(filePath);

        RobotOutParams outParams1 = new RobotOutParams();
        outParams1.setPins_pin_name("creditCode");
        outParams1.setPins_pin_defval(loginDto.getCreditCode());

        RobotOutParams outParams2 = new RobotOutParams();
        outParams2.setPins_pin_name("id");
        outParams2.setPins_pin_defval(fileId);
        RobotOutParams outParams3 = new RobotOutParams();
        outParams3.setPins_pin_name("sbYear");
        outParams3.setPins_pin_defval(sbYear);
        RobotOutParams outParams4 = new RobotOutParams();
        outParams4.setPins_pin_name("sbYearType");
        outParams4.setPins_pin_defval(sbYearType);
        RobotOutParams outParams5 = new RobotOutParams();
        outParams5.setPins_pin_name("companyName");
        outParams5.setPins_pin_defval(loginDto.getCompanyName());

        RobotOutParams outParams6 = new RobotOutParams();
        outParams6.setPins_pin_name("fileName");
        outParams6.setPins_pin_defval(fileName);

        robot_ipars.add(outParams0);
        robot_ipars.add(outParams6);
        robot_ipars.add(outParams1);
        robot_ipars.add(outParams5);
        robot_ipars.add(outParams2);
        robot_ipars.add(outParams3);
        robot_ipars.add(outParams4);

        RobotProjectInfoBean projectInfoBean = new RobotProjectInfoBean();
        projectInfoBean.setRobot_iscon("0");
        String robotName = "";
        if("年度".equalsIgnoreCase(sbYearType)){
            if(fileName.contains("财务报表2019")){
                robotName = "年度-财务报表2019";
            }else if(fileName.contains("财务报表2013")){
                robotName = "年度-财务报表2013";
            }else if(fileName.contains("财务报表2007")){
                robotName = "年度-财务报表2007";
            }else{
                robotName = "年度-企业所得税";
            }
        }else if("季度".equalsIgnoreCase(sbYearType)){
            if(fileName.contains("财务报表2019")){
                robotName = "季度-财务报表2019";
            }else if(fileName.contains("财务报表2013")){
                robotName = "季度-财务报表2013";
            }else if(fileName.contains("财务报表2007")){
                robotName = "季度-财务报表2007";
            }else{
                robotName = "季度-企业所得税";
            }
        }else{
            if(fileName.contains("财务报表2019")){
                robotName = "月度-财务报表2019";
            }else if(fileName.contains("财务报表2013")){
                robotName = "月度-财务报表2013";
            }else if(fileName.contains("财务报表2007")){
                robotName = "月度-财务报表2007";
            }
        }
        projectInfoBean.setRobot_name(robotName);
        //projectInfoBean.setRobot_ver("1.0.0.0");
        projectInfoBean.setRobot_ipars(robot_ipars);
        task_robots.add(projectInfoBean);

        RobotPlanBean planBean = new RobotPlanBean();
        planBean.setTask_robots(task_robots);
        planBean.setTask_name("税智报报送任务");
        String strDate = DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
        planBean.setTask_ontime(strDate);
        planBean.setTask_isVideo(3);
        planBean.setTask_overTime(1440);
        planBean.setTask_logLevel("3");
        planBean.setTask_runTime("0");
        planBean.setTask_reCount("0");
        data.add(planBean);

       try {
           String paras = JSONUtil.toJsonStr(params);
           //new MyAlert(Alert.AlertType.INFORMATION, paras).show();http://127.0.0.1:56001
           String pushUrl = loginDto.getIpAddr();
           return  HttpClientUtil.postJson(pushUrl,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
